//
//  InterfaceNetManager.m
//  JiuLe
//
//  Created by xcq on 15/10/31.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "UserDataMarco.h"
#import "InterfaceNetManager.h"
#import "JlUserManager.h"

@implementation InterfaceNetManager

+ (void)RequestLogin:(NSString *)userName
            PassWord:(NSString *)pwd
          completion:(interfaceManagerBlock)completion {
    NSDictionary *dic = @{@"username":userName, @"password":pwd};
    [self postHTTPServerWith:dic
                  completion:completion
                      method:Api_Login];
}

+ (void)RequestHomePageDataCompletion:(interfaceManagerBlock)completion {
    JlUserManager *user = [JlUserManager shareInstance];
    
    NSDictionary *dic = @{@"username":user.userInfo.userid};
    [self postHTTPServerWith:dic
                  completion:completion
                      method:Api_homePage];
}

+ (void)RequestUserNewMessage:(interfaceManagerBlock)completion DateStr:(NSString *)date{
    JlUserManager *user = [JlUserManager shareInstance];
    NSDictionary *dic = @{@"username":user.userInfo.userid,@"time":date == nil ? @"" : date};
    [self postHTTPServerWith:dic
                  completion:completion
                      method:Api_message];
}

+ (void)RequestActivateDevice:(interfaceManagerBlock)completion
                 andParameter:(NSDictionary *)parameter{
    [self postHTTPServerWith:parameter
                  completion:completion
                      method:Api_DeviceActivate];
}

+ (void)RequestDeviceInfo:(interfaceManagerBlock)completion {
    JlUserManager *user = [JlUserManager shareInstance];
    NSDictionary *dic = @{@"username":user.userInfo.userid};
    [self postHTTPServerWith:dic
                  completion:completion
                      method:Api_QueryDevice];
}

+ (void)MessageReadStatusChange:(NSString *)messageId completion:(interfaceManagerBlock)completion {
    NSDictionary *dic = @{@"messageId":messageId};
    
    [self postHTTPServerWith:dic completion:completion
                      method:Api_ChangeMessageRead];
}

+ (void)RequestClockSettingCompletion:(interfaceManagerBlock)completion {
    JlUserManager *user = [JlUserManager shareInstance];
    NSDictionary *dic = @{@"userid":user.userInfo.userid,
                          };
    
    [self getHTTPServerWith:dic
                 completion:completion
                     method:Api_GetClock];
}


+ (void)RequestPhoneSettingCompletion:(interfaceManagerBlock)completion{
    JlUserManager *user = [JlUserManager shareInstance];
    NSDictionary *dic = @{@"userid":user.userInfo.userid};

    [self getHTTPServerWith:dic
                 completion:completion
                     method:Api_GetPhone];
}


+ (void)RequestHealthSettingCompletion:(interfaceManagerBlock)completion{
    JlUserManager *user = [JlUserManager shareInstance];
    
    NSDictionary *dic = @{@"userid":user.userInfo.userid};
    [self getHTTPServerWith:dic
                 completion:completion
                     method:Api_GetHealth];
//    
//    [self postHTTPServerWith:dic
//                  completion:completion
//                      method:Api_GetHealth];
}

+ (void)SetClockSetting:(interfaceManagerBlock)completion
           andParameter:(NSDictionary *)parameter{
    [self postHTTPServerWith:parameter
                  completion:completion
                      method:Api_SetClock];
}

+ (void)SetHealthSetting:(interfaceManagerBlock)completion
            andParameter:(NSDictionary *)parameter{
    [self postHTTPServerWith:parameter
                  completion:completion
                      method:Api_SetHealth];
}

+ (void)SetPhoneSetting:(interfaceManagerBlock)completion
           andParameter:(NSDictionary *)parameter{
    [self postHTTPServerWith:parameter
                  completion:completion
                      method:Api_SetPhone];
}

#pragma mark - 发送请求的公共方法:POST请求
+ (void) postHTTPServerWith:(NSDictionary *)param
                 completion:(interfaceManagerBlock)completion
                     method:(NSString *)method
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
        return;
    }
    JLLog(@"api:%@",method);
    NSString *url =  [[KBaseURL stringByAppendingString:@"/"] stringByAppendingString:method];
    [NetManager postWithURL:url params:param success:^(id json) {
        BOOL isSuccess = [[json objectForKey:@"success"] boolValue];
        if (isSuccess) {
            completion(YES, @"成功", [json objectForKey:@"result"] , nil);
        } else {
            completion(NO, [json objectForKey:@"error_message"], [json objectForKey:@"result"] , nil);
        }
    } failure:^(NSError *error) {
        completion(NO, [self analyzeError:error], nil , error);
    }];
}

+ (void) getHTTPServerWith:(NSDictionary *)param completion:(interfaceManagerBlock)completion method:(NSString *)method {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
        return;
    }
    NSString *url =  [[KBaseURL stringByAppendingString:@"/"] stringByAppendingString:method];
    [NetManager getWithURL:url params:param success:^(id json) {
        BOOL isSuccess = [[json objectForKey:@"success"] boolValue];
        if (isSuccess) {
            if ([json objectForKey:@"result"] != nil) {
                completion(YES, @"成功", [json objectForKey:@"result"] , nil);
            } else {
                completion(YES, @"成功", [json objectForKey:@"data"] , nil);
            }
        } else {
            completion(NO, [json objectForKey:@"error_message"], [json objectForKey:@"result"] , nil);
        }
    } failure:^(NSError *error) {
        completion(NO, [self analyzeError:error], nil , error);
    }];
}

+ (NSString *)analyzeError:(NSError *)error {
    NSString *errorMsg = @"";
    switch (error.code) {
        case NSURLErrorTimedOut:
            errorMsg = @"请求超时，请重试!";
            break;
        case NSURLErrorUnknown:
            errorMsg = @"未知错误!";
            break;
        case NSURLErrorCancelled:
            errorMsg = @"请求取消!";
            break;
        default:
            errorMsg = error.localizedDescription;
            break;
    }
    return errorMsg;
}

@end
